<?php

$action = isset($_GET['action']) ? $_GET['action'] : '';

$username = isset($_POST['username']) ? $_POST['username'] : '';
$userpsw = isset($_POST['userpsw']) ? $_POST['userpsw'] : '';
$userpsw1 = isset($_POST['userpsw1']) ? $_POST['userpsw1'] : '';
$code = isset($_POST['code']) ? $_POST['code'] : '';
$email = isset($_POST['useremail']) ? $_POST['useremail'] : '';
$url = isset($_POST['usernet']) ? $_POST['usernet'] : '';
$articles = isset($_POST['textarea']) ? $_POST['textarea'] : '';


$oldpsw = isset($_POST['old_psw']) ? $_POST['old_psw'] : ''; // 更换密码
$newpsw = isset($_POST['new_psw1']) ? $_POST['new_psw1'] : ''; // 更换密码
$name = isset($_GET['name']) ? $_GET['name'] : ''; // 更换密码

$pic = isset($_POST['set_pic']) ? $_POST['set_pic'] : ''; // 设置头像

try {
    $dsn = 'mysql:host=localhost;dbname=yunbbs;charset=utf8';
    $pdo = new PDO($dsn, 'root', '123456', array(
        PDO:: ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ));
} catch (PDOException $ex) {
    echo '链接数据库失败';
}
switch ($action) {
    case 'reginst': {
            session_start();
            $code1 = $_SESSION['verifyCode'];
            if ($code !== $code1) {
                echo "验证码输入错误";
                exit();
            }
            if (!$pdo->query("select name from yunbbs_users where name='$username'")) {
                echo '账号已存在,请<a href="reginst.php" style="text-decoration: none;">重新注册</a>';
                exit();
            }
            $regtime = time();
            $userpsw = md5($userpsw);
            $avatar = 'image/1.png';
            $pdo->exec("insert into yunbbs_users (name, password, regtime, avatar) value ('$username', '$userpsw', '$regtime', '$avatar')");
            echo '注册成功 <a href="login.php" style="text-decoration: none;">现在登录</a>';
        }
        break;
    case 'login': {
            if (!$pdo->query("select * from yunbbs_users where name='$username'")) {
                echo '用户名不存在,' . '<a href="reginst.php" style="text-decoration: none;">请先注册</a>';
                exit();
            }
            $users = $pdo->query("select * from yunbbs_users where name='$username'");
            foreach ($users as $key => $value) {
//                print_r($value);
            }
            $userpsw = md5($userpsw);
            if ($userpsw !== $value['password']) {
                echo '密码输入错误,请' . '<a href="login.php" style="text-decoration: none;">重新登录</a>';
                exit();
            }
            header("Location: index.php?name=$username");
            setcookie('name', $username, time() + 7 * 24 * 60 * 60);
            setcookie('psw', $userpsw, time() + 7 * 24 * 60 * 60);
        }
        break;
    case 'set_personal_info': {
            $result = $pdo->exec("update yunbbs_users set email = '$email', url = '$url', articles = '$articles'  where name='$name'");
            echo '设置个人信息成功' . ' <a href="index.php" style="text-decoration: none;">返回主页</a>' . ' <a href="set_bbs.php" style="text-decoration: none;">继续设置</a>';
        }
        break;
    case 'set_pic': {
            print_r($_FILES);
        }
        break;
    case 'set_psw': {
            $userpsw = $pdo->query("select password from yunbbs_users where name='$name'");
            foreach ($userpsw as $key => $value) {
                
            }
            $oldpsw = md5($oldpsw);
            $newpsw = md5($newpsw);
            if ($value['password'] !== $oldpsw) {
                echo '旧密码输入错误,请重新设置' . '<a href="set_bbs.php" style="text-decoration: none;">返回设置</a>';
                exit();
            }
            $result = $pdo->exec("update yunbbs_users set password = '$newpsw' where name='$name'");
            if ($result == 0) {
                echo '请输入不同的密码' . ' <a href="set_bbs.php" style="text-decoration: none;">返回继续</a>';
                exit();
            }
            echo '设置密码成功' . ' <a href="index.php" style="text-decoration: none;">返回主页</a>' . ' <a href="set_bbs.php" style="text-decoration: none;">继续设置</a>';
        }
        break;
    case 'posting': {
            $addtime = time();  // 发帖时间
            $title = isset($_POST['title']) ? $_POST['title'] : '';
            $content = isset($_POST['bbs_text']) ? $_POST['bbs_text'] : '';
            $uid = isset($_GET['id']) ? $_GET['id'] : '';
//            print_r($_POST);
//        $edittime = time();  // 编辑时间
//        $views = ; // 查看人数
//        $comments = ; // 评论人数
//        $favorites  // 收藏人数
            $send_bbs = $pdo->query("insert into yunbbs_articles (addtime, title, content, uid ) value ('$addtime', '$title', '$content', '$uid')");
            if ($send_bbs <= 0) {
                echo '发表帖子失败' . '<a href="posting.php" style="text-decoration: none;">重新发帖</a>';
                exit();
            }
            header("Location: bbs_list.php?name=$name");
        }
        break;
    case 'fenlei_info': {
            $name = isset($_POST['new_fenlei']) ? $_POST['new_fenlei'] : '';
            $article = isset($_POST['fenlei_info']) ? $_POST['fenlei_info'] : '';
            if($name == '' && $article == '') {
                echo '输入不能为空'.'<a href="fenleiguanli.php" style="text-decoration: none;">重新添加</a>';
                exit();
            }
            $set_fenlei = $pdo->query("insert into yunbbs_categories (name, articles) value ('$name', '$article')");
            if ($set_fenlei <= 0) {
                echo '添加分类失败' . '<a href="fenleiguanli.php" style="text-decoration: none;">重新添加</a>';
                exit();
            }
            header("Location: index.php?name = $name");
        }
        break;
    default:
        break;
}
    